/*
 * @Author: zi.yang
 * @Date: 2023-11-24 13:02:39
 * @LastEditors: zi.yang
 * @LastEditTime: 2023-11-25 16:27:57
 * @Description: 爬取百度百科中的地图信息
 * @FilePath: /node-classical/demo/map.baike.js
 */
import got from 'got';
import { JSDOM } from 'jsdom';

const baseUrl = 'https://baike.baidu.com'
const res = await got.get(baseUrl + '/item/%E5%8C%97%E4%BA%AC%E5%B8%82/126069')


const dom = new JSDOM(res.body)?.window.document;


const table = dom.querySelector('[label-module="lemmaSummary"]');

// console.log(table.textContent);

const tableList = dom.querySelectorAll('table');
console.log(dom.querySelector('table'));

let l = null;
tableList.forEach(element => {
  if (element.querySelector('caption')?.textContent === '区划') {
    l = element
  }
});

if (!l) {
  console.log('l is null');
  process.exit();
}
const regionLinks = l?.querySelectorAll('tr > td:nth-of-type(1) a')
const regionList = [];
regionLinks.forEach((link) => {
  const href = link.getAttribute('href');
  const text = link.textContent;
  regionList.push({ href, text })
})

console.log(regionList);
const allRequest = async (listRequest) => {
  const results = await Promise.allSettled(listRequest);
  return results.map(({ value }) => value)
}

const regions = await allRequest(regionList.map(({ href }) => got.get(`${baseUrl}${href}`)))

const streets = {};
regions.forEach((item, idx) => {
  if (!item) return
  const regionDom = new JSDOM(item.body)?.window.document;
  const tables = regionDom.querySelectorAll('table')
  const tableCount = tables.length;
  for (let i = 0; i < tableCount; i++) {
    const firstTd = tables.item(i).querySelector('div');
    // console.log(firstTd.textContent);
    if (['地名代码', '统计用区划代码'].includes(firstTd?.textContent)) {
      streets[regionList[idx].text] = tables.item(i);
      break;
    }
  }
})

console.log(streets);
